package per.dg.digital_klotski;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import per.dg.digital_klotski.utils.FontDisplayUtil;
import per.dg.digital_klotski.utils.SharedPreferencesManager;
import per.dg.digital_klotski.views.App;
import per.dg.digital_klotski.views.DigitalKlotskiManager;
import per.dg.digital_klotski.views.base.BaseActivity;

public class MainActivity extends BaseActivity {

    int space = 3;

    DigitalKlotskiViewGroup digitalKlotskiViewGroup;

    Button bt;

    AlertDialog alertDialog = null;

    SharedPreferencesManager sharedPreferencesManager;

    ImageView imgSound, imgMusic;

    TextView tvTime;

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((App)getApplication()).starttip();
            switch (v.getId()){
                case R.id.img_back:
                    finishActivity();
                    break;
                case R.id.img_refresh:
                    refresh();
                    break;
                case R.id.img_sound:
                    soundClick();
                    break;
                case R.id.img_music:
                    musicClick();
                    break;
            }
        }
    };

    long startTime = 0;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    int time = (int) ((System.currentTimeMillis() - startTime)/1000);
                    StringBuffer stringBuffer = new StringBuffer();
                    if(time/60 < 10){
                        stringBuffer.append("0");
                    }
                    stringBuffer.append(time/60);
                    stringBuffer.append(":");
                    if(time%60 < 10){
                        stringBuffer.append("0");
                    }
                    stringBuffer.append(time%60);
                    tvTime.setText(stringBuffer.toString());
                    handler.sendEmptyMessageDelayed(0, 500);
                    break;
            }
        }
    };

    private void finishActivity(){
        finish();
    }

    private void refresh(){
        digitalKlotskiViewGroup.setSpac(space);
        startTiming();
    }

    private void startTiming(){
        tvTime.setText("00:00");
        startTime = System.currentTimeMillis();
        handler.sendEmptyMessageDelayed(0, 500);
    }

    private void timed(){
        handler.removeCallbacksAndMessages(null);
    }

    DigitalKlotskiViewGroup.DigitalKlotskiChange digitalKlotskiChange = new DigitalKlotskiViewGroup.DigitalKlotskiChange() {
        @Override
        public void result(boolean reslut) {
            if(reslut){
                timed();
                showDialog();
            }
        }

        @Override
        public void onChildViewClick() {
            ((App)getApplication()).starttip();
        }
    };

    private void showDialog(){

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                refresh();
            }
        });
        View inflate = LayoutInflater.from(this).inflate(R.layout.layout_dialog_win, null);
        inflate.findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((App)getApplication()).starttip();
                if(alertDialog != null)
                    alertDialog.dismiss();
                finishActivity();
            }
        });
        inflate.findViewById(R.id.img_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((App)getApplication()).starttip();
                if(alertDialog != null)
                    alertDialog.dismiss();
                refresh();
            }
        });
        int width = FontDisplayUtil.dip2px(this, 245);
        int height = FontDisplayUtil.dip2px(this, 243);

        builder.setView(inflate);
//        inflate.setBackground(null);
//        inflate.setBackground(getResources().getDrawable(R.mipmap.win_sheet));
        alertDialog = builder.create();
//        alertDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        alertDialog.getWindow().setBackgroundDrawableResource(R.mipmap.win_sheet);
        alertDialog.setCanceledOnTouchOutside(false);

        alertDialog.show();
        WindowManager.LayoutParams params = alertDialog.getWindow().getAttributes();
        params.width = width;
        params.height = height;
        alertDialog.getWindow().setAttributes(params);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = getIntent();
        space = intent.getIntExtra("space", 3);

        digitalKlotskiViewGroup = findViewById(R.id.aa);
        digitalKlotskiViewGroup.setSpac(space);
        digitalKlotskiViewGroup.setDigitalKlotskiChange(digitalKlotskiChange);
        bt = findViewById(R.id.bt_remove);

        findViewById(R.id.img_back).setOnClickListener(onClickListener);
        findViewById(R.id.img_refresh).setOnClickListener(onClickListener);
        imgSound = findViewById(R.id.img_sound);
        imgSound.setOnClickListener(onClickListener);
        imgMusic = findViewById(R.id.img_music);
        imgMusic.setOnClickListener(onClickListener);

        tvTime = findViewById(R.id.tv_time);

        sharedPreferencesManager = SharedPreferencesManager.getInstance(this);

        setMusicData();
        setSoundData();
        tvTime.setText("00:00");
    }

    @Override
    protected void onResume() {
        super.onResume();
        startTiming();
    }

    public void onclick_add(View view) {
        if(space < 10){
            space++;
            digitalKlotskiViewGroup.setSpac(space);
            bt.performClick();
            bt.callOnClick();
        }

    }

    public void onclick_remove(View view) {
        if(space > 3){
            space--;
            digitalKlotskiViewGroup.setSpac(space);
        }
    }

    private void musicClick(){
        sharedPreferencesManager.saveMusic(!sharedPreferencesManager.getMusic());
        setMusicData();
        ((App)getApplication()).startBgm();
    }

    private void soundClick(){
        sharedPreferencesManager.saveSound(!sharedPreferencesManager.getSound());
        setSoundData();
    }

    private void setSoundData() {
        imgSound.setImageDrawable(getResources().getDrawable(sharedPreferencesManager.getSound()? R.mipmap.btn_sound_on_normal_2 : R.mipmap.btn_sound_off_normal_2));
    }

    private void setMusicData() {
        imgMusic.setImageDrawable(getResources().getDrawable(sharedPreferencesManager.getMusic()? R.mipmap.btn_music_on_normal_2 : R.mipmap.btn_music_off_normal_2));
    }
}